package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(timeout = 500000)
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public long save(Comment comment) {
        //1、保存评论；db_quanzi_comment
        comment.setId(ObjectId.get());//可选
        mongoTemplate.save(comment);

        //2、修改发布动态表,点赞数量+1；quanzi_publish
        //2.1 修改条件：动态id
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        //2.2 修改内容：如likeCount+1
        Update update = new Update();
        //update.inc("likeCount",1);
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query,update, Publish.class);

        //3、返回点赞数量；查询Comment
        Query commentQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
        );
        long count = mongoTemplate.count(commentQuery, Comment.class);
        return count;
    }

    @Override
    public long delete(Comment comment) {
        //1、执行删除评论数据
        Query query = new Query(
               Criteria.where("publishId").is(comment.getPublishId())
               .and("commentType").is(comment.getCommentType())
               .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query,Comment.class);

        //2、修改发布动态表，点赞数量减1
        Query publisQuery = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1);
        mongoTemplate.updateFirst(publisQuery,update, Publish.class);

        //3、查询点赞总数并返回
        Query commentQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );
        long count = mongoTemplate.count(commentQuery, Comment.class);
        return count;
    }

    //接口名称：评论列表
    //db.quanzi_comment.find({publishId:ObjectId('xx'),commentType:2}).sort({created:-1})
    @Override
    public PageResult queryCommentsList(String movementId, Integer page, Integer pagesize) {
        //1. 创建查询对象
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(2)
        );
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));

        //2. 分页查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        //3. 封装分页结果并返回
        return new PageResult(page,pagesize, (int) count,commentList);
    }

    /**
     * 喜欢列表
     */
    @Override
    public PageResult queryLoveList(Integer page, Integer pagesize, Long userId, Integer commentType) {
        Query query = new Query(Criteria.where("publishUserId").is(userId)
                                .and("commentType").is(commentType));
        query.limit(pagesize).skip((page - 1 )*pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        //3. 封装分页结果并返回
        return new PageResult(page,pagesize, (int) count,commentList);
    }

    /**
     *点赞列表
     */
    @Override
    public PageResult querylikeList(Integer page, Integer pagesize, Long userId, Integer commentType) {
        Query query = new Query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType));
        query.limit(pagesize).skip((page - 1 )*pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        //3. 封装分页结果并返回
        return new PageResult(page,pagesize, (int) count,commentList);
    }

    /**
     *评论列表
     */
    @Override
    public PageResult querycommentsList(Integer page, Integer pagesize, Long userId, Integer commentType) {
        Query query = new Query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType));
        query.limit(pagesize).skip((page - 1) * pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        //3. 封装分页结果并返回
        return new PageResult(page, pagesize, (int) count, commentList);

    }

    /**
     * 视频评论列表
     * @param videoId
     * @param
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult queryVideoCommentList(String videoId, Integer page, Integer pagesize) {
        //1. 创建查询对象
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(videoId))
                        .and("commentType").is(2)
        );
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));

        //2. 分页查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        //3. 封装分页结果并返回
        return new PageResult(page,pagesize, (int) count,commentList);
    }

    @Override
    public void savevideoComment(Comment commrnt1) {
        mongoTemplate.save(commrnt1);

    }


}
